package com.whfc.wxmp.api.sys.service.impl;

import com.whfc.common.enums.AppType;
import com.whfc.common.enums.EnableState;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.weixin.bean.OAuthUserInfo;
import com.whfc.common.weixin.mp.LoginApi;
import com.whfc.common.weixin.mp.SessionKey;
import com.whfc.fuum.dto.WxConfigDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.entity.WxUser;
import com.whfc.fuum.service.SysUserService;
import com.whfc.fuum.service.WxUserService;
import com.whfc.wxmp.api.base.service.WxConfigLocalService;
import com.whfc.wxmp.api.sys.service.WxLoginService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/7/22 11:29
 */
@Service
public class WxLoginServiceImpl implements WxLoginService {

    @Autowired
    private WxConfigLocalService configService;

    @DubboReference(interfaceClass = SysUserService.class, version = "1.0.0")
    private SysUserService sysUserService;

    @DubboReference(interfaceClass = WxUserService.class, version = "1.0.0")
    private WxUserService wxUserService;


    @Override
    public WxUser wxMpLogin(AppType appType, String openId, String code) {
        WxUser wxUser = null;
        //测试用,openId登陆
        if (!StringUtils.isEmpty(openId)) {
            wxUser = wxUserService.getUserByOpenId(openId);
        }
        //小程序登陆
        else {
            WxConfigDTO configDTO = configService.getWxConfig(appType);
            SessionKey sessionKey = LoginApi.code2session(configDTO.getAppId(), configDTO.getAppSecret(), code);
            if (sessionKey == null) {
                throw new BizException(ResultEnum.FAILURE.getCode(), "获取sessionKey失败");
            }
            wxUser = wxUserService.saveUser(appType, sessionKey.getOpenid(), sessionKey.getUnionid(), sessionKey.getSession_key());
        }

        return wxUser;
    }

    @Override
    public WxUser wxMpUpdateUserInfo(WxUser wxUser, String encryptedData, String iv) {
        return wxUserService.updateUserInfo(wxUser, encryptedData, iv);
    }

    @Override
    public WxUser wxMpUpdateUserInfo(WxUser wxUser, OAuthUserInfo userInfo) {
        wxUser.setNickName(userInfo.getNickname());
        wxUser.setAvatarUrl(userInfo.getHeadimgurl());
        wxUserService.updateUser(wxUser);
        return wxUser;
    }

    @Override
    public WxUser wxMpUpdateUser(WxUser wxUser, String username, String password) {

        SysUser user = sysUserService.getUserByUsername(username);
        if (user == null) {
            throw new AuthenticationException("账号或密码错误");
        }
        if (EnableState.DISABLED.getValue().equals(user.getStatus())) {
            throw new AuthenticationException("账号已被禁用");
        }
        String encryptedPassword = new SimpleHash("MD5", password, user.getSalt()).toString();
        if (!user.getPassword().equalsIgnoreCase(encryptedPassword)) {
            throw new AuthenticationException("账号或密码错误");
        }

        wxUser.setName(username);
        wxUser.setPhone(user.getPhone());
        wxUserService.updateUser(wxUser);
        return wxUser;
    }
}
